package top.soulblack.purchasesale.controller.setting;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import top.soulblack.purchasesale.common.Result;
import top.soulblack.purchasesale.model.GoodsBrand;
import top.soulblack.purchasesale.model.GoodsMaterial;
import top.soulblack.purchasesale.service.GoodsMaterialService;

import static top.soulblack.purchasesale.common.CodeMsg.*;

/**
 * create by 小川
 */
@Controller
@RequestMapping("/material")
public class GoodsMaterialController {

    @Autowired
    private GoodsMaterialService goodsMaterialService;

    @ResponseBody
    @RequestMapping("/addMaterial")
    public Result<Boolean> addMaterial(GoodsMaterial goodsMaterial) {
        // 判断是否存在当前品牌
        if (goodsMaterialService.existMaterial(goodsMaterial.getName())) {
            return Result.error(MATERIAL_EXIST);
        }
        goodsMaterialService.addMaterial(goodsMaterial);
        return Result.success(true);
    }

    @ResponseBody
    @RequestMapping("/editMaterial")
    public Result<Boolean> editMaterial(GoodsMaterial goodsMaterial) {
        System.out.println(goodsMaterial.getId());
        if (goodsMaterialService.updateMaterial(goodsMaterial)) {
            return Result.success(true);
        }
        return Result.error(MATERIAL_EDIT_ERROR);
    }

    @ResponseBody
    @RequestMapping("/deleteMaterial")
    // 后期需要加上User权限验证，否则暴露接口有危险。
    public Result<Boolean> deleteMaterial(int materialId) {
        System.out.println(materialId);
        if (goodsMaterialService.selectMaterial(materialId)){
            return Result.error(MATERIAL_LIFE);
        }
        if (goodsMaterialService.deleteMaterial(materialId)) {
            return Result.success(true);
        }
        return Result.error(MATERIAL_DELETE);
    }
}
